﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using DashFire;
using UnityEngine;
using UnityEditor;

namespace DashFire.Assetbundle
{
  internal class ExportRes_VersionInfo
  {
    internal static string s_VersionClientFile = "ClientVersion.txt";
    internal static string s_VersionServerFile = "ServerVersion.txt";

    public static bool ExportAll()
    {
      ExportClient();
      ExportServer();
      return true;
    }
    public static bool ExportClient()
    {
      ExportConfig.Log("--------------------------------------------------------------");
      ExportConfig.Log("ExportRes_VersionInfo.ExportClient Start...");

      string clientfilePath = ExportConfig.GetFilePathAbs(
        string.Format("{0}/{1}", ExportConfig.GetPlatformPath(), s_VersionClientFile));
      try {
        string dir = Path.GetDirectoryName(clientfilePath);
        if (!Directory.Exists(dir)) {
          Directory.CreateDirectory(dir);
        }
        if (File.Exists(clientfilePath)) {
          File.Delete(clientfilePath);
        }
        if (!File.Exists(clientfilePath)) {
          FileStream fs = File.Create(clientfilePath);
          fs.Close();
        }

        // ClientVersion.txt
        ConfigFile m_IniFile = new ConfigFile(clientfilePath);
        m_IniFile.AddSetting("AppSetting", "Version", ExportConfig.s_Version);
        m_IniFile.AddSetting("AppSetting", "Platform", ExportConfig.GetPlatformName(ExportConfig.s_BuildOptionTarget));
        m_IniFile.AddSetting("AppSetting", "GroupName", ExportConfig.s_GroupName);
        m_IniFile.AddSetting("AppSetting", "ResServerURL", ExportConfig.s_ResServerURL);

        m_IniFile.AddSetting("Runtime", "IsResVersionConfigCached", false.ToString());
        m_IniFile.AddSetting("Runtime", "IsAssetDBConfigCached", false.ToString());
        m_IniFile.AddSetting("Runtime", "IsResCacheConfigCached", false.ToString());
        m_IniFile.AddSetting("Runtime", "IsResSheetConfigCached", false.ToString());
        m_IniFile.SaveSettings();
      } catch (System.Exception ex) {
        ResBuildLog.Warn("VersionGenerator.GenVersionFile file failed!" + ex);
        return false;
      }

      AssetDatabase.Refresh();
      ExportConfig.Log("ExportRes_VersionInfo.ExportClient Success");
      return true;
    }
    public static bool ExportServer()
    {
      ExportConfig.Log("--------------------------------------------------------------");
      ExportConfig.Log("ExportRes_VersionInfo.ExportServer Start...");

      string serverfilePath = ExportConfig.GetFilePathAbs(
        string.Format("{0}/{1}", ExportConfig.GetPackagePath(), s_VersionServerFile));
      try {
        string dir = Path.GetDirectoryName(serverfilePath);
        if (!Directory.Exists(dir)) {
          Directory.CreateDirectory(dir);
        }
        if (File.Exists(serverfilePath)) {
          File.Delete(serverfilePath);
        }
        if (!File.Exists(serverfilePath)) {
          FileStream fs = File.Create(serverfilePath);
          fs.Close();
        }

        // ServerVersion.txt
        ConfigFile m_IniFile = new ConfigFile(serverfilePath);
        m_IniFile.AddSetting("AppSetting", "Version", ExportConfig.s_Version);
        m_IniFile.AddSetting("AppSetting", "Platform", ExportConfig.GetPlatformName(ExportConfig.s_BuildOptionTarget));
        m_IniFile.AddSetting("AppSetting", "GroupName", ExportConfig.s_GroupName);
        m_IniFile.AddSetting("AppSetting", "ResServerURL", ExportConfig.s_ResServerURL);
        m_IniFile.AddSetting("AppSetting", "ForceDownloadURL", ExportConfig.GetForceDownloadUrl(ExportConfig.s_BuildOptionTarget));

        m_IniFile.AddSetting("Runtime", "IsResVersionConfigCached", false.ToString());
        m_IniFile.AddSetting("Runtime", "IsAssetDBConfigCached", false.ToString());
        m_IniFile.AddSetting("Runtime", "IsResCacheConfigCached", false.ToString());
        m_IniFile.AddSetting("Runtime", "IsResSheetConfigCached", false.ToString());
        m_IniFile.SaveSettings();
      } catch (System.Exception ex) {
        ResBuildLog.Warn("ExportRes_VersionInfo.GenVersionFile file failed!" + ex);
        return false;
      }

      AssetDatabase.Refresh();
      ExportConfig.Log("VersionGenerator.ExportServer Success");
      return true;
    }

    #region MenuItem
    [MenuItem("Export/Export Selected/4.Version Info/Export All Version Info", false, 61)]
    internal static void ExportAllVersionInfo()
    {
      if (ExportConfig.Load()
        && ExportAll()) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportAllVersionInfo Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportAllVersionInfo Failed!",
          "OK");
      }
    }
    [MenuItem("Export/Export Selected/4.Version Info/Export Version Info", false, 62)]
    internal static void ExportClientVersionInfo()
    {
      if (ExportConfig.Load()
        && ExportClient()) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportClientVersionInfo Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportClientVersionInfo Failed!",
          "OK");
      }
    }
    [MenuItem("Export/Export Selected/4.Version Info/Export Server Version Info", false, 63)]
    internal static void ExportServerVersionInfo()
    {
      if (ExportConfig.Load()
        && ExportServer()) {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportServerVersionInfo Success!",
          "OK");
      } else {
        EditorUtility.DisplayDialog(
          "Confirm",
          "ExportServerVersionInfo Failed!",
          "OK");
      }
    }
    [MenuItem("Assets/Export Selected/4.Version Info/Export All Version Info", false, 61)]
    internal static void Assets_ExportAllVersionInfo()
    {
      ExportAllVersionInfo();
    }
    [MenuItem("Assets/Export Selected/4.Version Info/Export Version Info", false, 62)]
    internal static void Assets_ExportClientVersionInfo()
    {
      ExportClientVersionInfo();
    }
    [MenuItem("Assets/Export Selected/4.Version Info/Export Server Version Info", false, 63)]
    internal static void Assets_ExportServerVersionInfo()
    {
      ExportServerVersionInfo();
    }
    #endregion
  }
}
